<?php

function diffBetweenTwoDays ($day1, $day2)
{
    $second1 = strtotime($day1);
    $second2 = strtotime($day2);

    if ($second1 < $second2) {
        $tmp = $second2;
        $second2 = $second1;
        $second1 = $tmp;
    }
    return ($second1 - $second2) / 86400;
}

function generate_code($length = 6) {
    return rand(pow(10,($length-1)), pow(10,$length)-1);
}

function diff_day($birthday){
    $date2 = $birthday;
    $date1 = date('Y-m-d');
    if(strtotime($date1)>strtotime($date2)){
        $tmp=$date2;
        $date2=$date1;
        $date1=$tmp;
    }
    list($Y1,$m1,$d1)=explode('-',$date1);
    list($Y2,$m2,$d2)=explode('-',$date2);
    $Y=$Y2-$Y1;
    $m=$m2-$m1;
    $d=$d2-$d1;
    if($d<0){
        $d+=(int)date('t',strtotime("-1 month $date2"));
        $m--;
    }
    if($m<0){
        $m+=12;
        $Y--;
    }

    return array('year'=>$Y,'month'=>$m, 'd' => $d);
}


function birthday($birthday, $is_round = 1){
    $arr = diff_day($birthday);
    if ($is_round) {
        if ($arr['month'] >= 6) {
            return ++$arr['year'];
        } else {
            return $arr['year'];
        }
    }
    return $arr;

    $strto_birthday = strtotime($birthday);
    $byear=date('Y',$strto_birthday);
    $bmonth=date('m',$strto_birthday);
    $bday=date('d',$strto_birthday);

    $tyear=date('Y');
    $tmonth=date('m');
    $tday=date('d');

    $age=$tyear-$byear;

    if($bmonth>$tmonth || ($bmonth==$tmonth && $bday>$tday)){
        $age--;
    }
    return $age;
}


function create_password($length = 6,$type = 'alpha-number')
{
    $code_arr = array(
        'alpha' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
        'number'=> '0123456789',
        'sign'  => '#$%@*-_',
    );

    $type_arr = explode('-',$type);

    foreach($type_arr as $t){
        if( ! array_key_exists($t,$code_arr)){
            trigger_error("Can not generate type ($t) code");
        }
    }

    $chars = '';

    foreach($type_arr as $t){
        $chars .= $code_arr[$t];
    }
    $chars = str_shuffle($chars);
    $number = $length > strlen($chars) - 1 ? strlen($chars) - 1:$length;
    return substr($chars,0,$number);
}

function json_encode_cn($arr = []) {
    return json_encode($arr, JSON_UNESCAPED_UNICODE);
}

function generateRandomString($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}

function format_yuan($fen, $decimal = 2) {
    return number_format($fen / 100, $decimal);
}

function create_order_no($prefix = '') {
    return $prefix . date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
}

function toCamelCase($str)
{
    $array = explode('_', $str);
    $result = $array[0];
    $len=count($array);
    if($len>1)
    {
        for($i=1;$i<$len;$i++)
        {
            $result.= ucfirst($array[$i]);
        }
    }
    return $result;
}

function per_page($default = 20) {
    return \request('per_page', $default);
}

function getip() {

    static $ip = '';

    $ip = $_SERVER['REMOTE_ADDR'];

    if(isset($_SERVER['HTTP_CDN_SRC_IP'])) {

        $ip = $_SERVER['HTTP_CDN_SRC_IP'];

    } elseif (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) {

        $ip = $_SERVER['HTTP_CLIENT_IP'];

    } elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {

        foreach ($matches[0] AS $xip) {

            if (!preg_match('#^(10|172\.16|192\.168)\.#', $xip)) {

                $ip = $xip;

                break;

            }

        }

    }

    return $ip;

}

function short_content($c1) {
    $c2 = htmlspecialchars_decode($c1);//把一些预定义的 HTML 实体转换为字符
    $c3 = str_replace("&nbsp;","",$c2);//将空格替换成空
    $contents = strip_tags($c3);//函数剥去字符串中的 HTML、XML 以及 PHP 的标签,获取纯文本内容
    return  subtext($contents, 30);
}


function subtext($text, $length)
{
    if (mb_strlen($text, 'utf8') > $length) {
        return mb_substr($text, 0, $length, 'utf8') . '...';
    } else {
        return $text;
    }

}